import {
  getSpuList,
  getAttributeList
} from '../../api/productApi.js'

export default {
  namespaced: true,
  state: {
    //购物数量
    buy_number: 1,
    //当前spu的第一个sku
    sku: [],
    //当前spu的第一个sku的属性
    sku_attr_list: [],
    //相册列表下标
    ablum_index: 0,
    //相册下标
    ablum_item_index: 0,
    //当前选中的商品,用于商品详情页
    spu: {},
    //首页 推荐商品列表
    home_recommend_spu_list: [],
    //首页 推荐分类列表
    home_recommend_category_list: [{
        cate_id: 26,
        cate_name: '4K超清电视'
      },
      {
        cate_id: 34,
        cate_name: '变频空调'
      },
      {
        cate_id: 130,
        cate_name: '手机'
      },
      {
        cate_id: 219,
        cate_name: '笔记本'
      }
    ],
    /*  SpuList 商品列表页面  */
    // 当前筛选的分类
    spulist_filter_cate: null,
    // 当前筛选的商品名称
    spulist_filter_spu_name: null,
    // 当前筛选的属性值列表
    spulist_filter_valueList: [],
    // 当前筛选的起始索引
    spulist_filter_start: 0,
    // 当前筛选的长度
    spulist_filter_length: 10,
    // 满足条件的商品列表
    spulist: [],
    // 满足条件的分类列表
    categoryList: [],
    // 满足条件的属性列表
    spuAttrList: [],
  },
  mutations: {
    //设置推荐商品列表
    home_recommend_spu_list(state, value) {
      state.home_recommend_spu_list.push(value)
    },
    //设置当前选中的商品
    set_spu(state, value) {
      state.spu = value
    },
    //设置相册列表
    set_ablum_index(state, value) {
      state.ablum_index = value
    },
    //设置相册下标
    set_ablum_item_index(state, value) {
      state.ablum_item_index = value
    },
    //设置当时spu的第一个sku
    set_sku(state, value) {
      state.sku = value
    },
    //设置当时spu的第一个sku的属性
    set_sku_attr_list(state, value) {
      state.sku_attr_list = value
    },
    /*  SpuList 商品Spu列表页面  */
    set_spulist_filter_spu_name(state, data) {
      state.spulist_filter_spu_name = data;
    },
    set_spulist_filter_cate(state, data) {
      state.spulist_filter_cate = data;
    },
    set_spulist_filter_valueList(state, data) {
      state.spulist_filter_valueList = JSON.parse(JSON.stringify(data));
    },
    set_spulist_filter_start(state, data) {
      state.spulist_filter_start = data;
    },
    set_spulist_filter_length(state, data) {
      state.spulist_filter_length = data;
    },
    set_spulist(state, data) {
      state.spulist = data;
    },
    set_categoryList(state, data) {
      state.categoryList = data;
    },
    set_spuAttrList(state, data) {
      // 商品属性 设置到 state
      state.spuAttrList = data;
    },
    append_spulist(state, data) {
      state.spulist.data = state.spulist.data.concat(data.data);
    }
  },
  actions: {
    /*  SpuList 商品Spu列表页面  */
    // 商品列表页面 查询满足条件的商品Spu列表和筛选列表
    getSpuAndFilterList(context) {
      // 准备请求参数
      let param = {};
      if (context.state.spulist_filter_spu_name != null) {
        param.spu_name = context.state.spulist_filter_spu_name;
      }
      if (context.state.spulist_filter_cate != null) {
        param.cate_id = context.state.spulist_filter_cate.cate_id;
      }
      if (context.state.spulist_filter_valueList != null &&
        context.state.spulist_filter_valueList.length > 0) {
        // 过滤 属性值中的null
        let arr = [];
        let v_index = 0;
        for (let v of context.state.spulist_filter_valueList) {
          if (v != null) {
            arr[v_index] = v;
            v_index++;
          }
        }
        param.valueList = arr.join();
      }
      param.start = context.state.spulist_filter_start;
      param.length = context.state.spulist_filter_length;
      // 调用webapi接口，查询满足条件的商品spu列表
      getSpuList(param).then(response => {
        // 设置 满足条件的商品列表
        context.commit('set_spulist', response.data);
        // 判断 是否有 筛选分类
        if (context.state.spulist_filter_cate == null) {
          // 无 筛选分类
          // 将 商品列表 的 分类列表 设置为 商品分类列表
          context.commit('set_categoryList', response.data.categoryList);
        }
      });
    },
    // 商品列表页面 查询满足条件的商品Spu列表 追加到页面中
    appendSpuList(context) {
      // 准备请求参数
      let param = {};
      if (context.state.spulist_filter_spu_name != null) {
        param.spu_name = context.state.spulist_filter_spu_name;
      }
      if (context.state.spulist_filter_cate != null) {
        param.cate_id = context.state.spulist_filter_cate.cate_id;
      }
      if (context.state.spulist_filter_valueList != null &&
        context.state.spulist_filter_valueList.length > 0) {
        // 过滤 属性值中的null
        let arr = [];
        let v_index = 0;
        for (let v of context.state.spulist_filter_valueList) {
          if (v != null) {
            arr[v_index] = v;
            v_index++;
          }
        }
        param.valueList = arr.join();
      }
      param.start = context.state.spulist_filter_start;
      param.length = context.state.spulist_filter_length;
      // 调用webapi接口，查询满足条件的商品spu列表
      getSpuList(param).then(response => {
        // 将满足条件的商品Spu列表 追加到state中
        context.commit('append_spulist', response.data);
      });
    },
    // 商品列表页面 查询 属性列表
    getSpuAttrList(context) {
      // 调用webapi接口
      getAttributeList({
        cate_id: context.state.spulist_filter_cate.cate_id,
        key_issku: 0,
        key_ishigh: 0,
      }).then(response => {
        // 设置 属性列表到 state
        context.commit('set_spuAttrList', response.data.data);
        // 设置 当前筛选的属性值
        let arr = [];
        for (let i = 0; i <= response.data.data.length - 1; i++) {
          arr[i] = null;
        }
        context.commit('set_spulist_filter_valueList', arr);
      });
    },
    /*  Home 首页部分  */
    // 查询首页推荐商品Spu列表
    get_home_recommend_spu_list(context) {
      context.state.home_recommend_category_list.forEach(cate => {
        getSpuList({
          cate_id: cate.cate_id,
          spu_status: 1,
          start: 0,
          length: 5
        }).then(response => {
          //当前推荐商品列表对象
          let recommend = {
            category: cate
          }
          recommend.spuList = response.data.data
          context.commit('home_recommend_spu_list', recommend)
        })
      })
    }
  },
  modules: {}
}
